package com.blb.zycommunity.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysUser;
import com.blb.zycommunity.entity.ZyRepair;
import com.blb.zycommunity.service.ISysUserService;
import com.blb.zycommunity.service.IZyRepairService;
import com.blb.zycommunity.utils.OSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * OSS文件上传
 */
@CrossOrigin
@RestController
public class UploadController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IZyRepairService iZyRepairService;

    @PostMapping("/upload/{username}")
    public ResponseResult<String> upload(@PathVariable String username, MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        OSSUtils.upload(file.getInputStream(),filename);
        String substring = OSSUtils.getURL(filename);
        String url = substring.substring(0, substring.indexOf("?"));
        userService.update(new UpdateWrapper<SysUser>().set("avatar",url).eq("user_name",username));
        System.out.println(url);
        return ResponseResult.ok(url);
    }

    /**
     * 微信上传图片(添加)
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/weixiupload")
    public ResponseResult weixiupload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        OSSUtils.upload(file.getInputStream(),filename);
        String substring = OSSUtils.getURL(filename);
        String url = substring.substring(0, substring.indexOf("?"));
        return ResponseResult.ok(url);
    }

    ///**
    // * 微信上传图片(添加)
    // * @param file
    // * @return
    // * @throws IOException
    // */
    //@PostMapping("uni-app/weixiuploadAdd")
    //public ResponseResult weixiuploadAdd(MultipartFile file) throws IOException {
    //    String filename = file.getOriginalFilename();
    //    OSSUtils.upload(file.getInputStream(),filename);
    //    String substring = OSSUtils.getURL(filename);
    //    return ResponseResult.ok(substring);
    //}

}